﻿using EBA.Collections;
using EBA.Contracts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace EBA.Reporting.Data
{
    /// <summary>
    /// 将<see cref="DataSet"/>对象包装成<see cref="IReportData"/>对象
    /// </summary>
    public class AdoReportData : IReportData
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dataSet">数据集</param>
        public AdoReportData(DataSet dataSet)
        {
            Contract.Requires<ArgumentNullException>(dataSet != null, "dataSet");
            this.DataSet = dataSet;
            this.Tables = new AdoReportDataTableCollection();

            for (var i = 0; i < this.DataSet.Tables.Count; i++)
            {
                var dataTable = new AdoReportDataTable(this.DataSet.Tables[i], i);
                this.Tables.Add(dataTable);
            }
        }

        DataSet DataSet { get; set; }

        /// <summary>
        /// 数据表集合
        /// </summary>
        AdoReportDataTableCollection Tables
        {
            get;
            set;
        }

        /// <summary>
        /// 按索引取表
        /// </summary>
        /// <param name="tableIndex"></param>
        /// <returns></returns>
        public IReportDataTable this[int tableIndex]
        {
            get
            {
                return this.Tables[tableIndex];
            }
        }

        /// <summary>
        /// 按表名取表
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public IReportDataTable this[string tableName]
        {
            get 
            {
                return this.Tables.Where(i => i.TableName == tableName).FirstOrDefault();
            }
        }

        IReadonlyList<IReportDataTable> IReportData.Tables
        {
            get { return this.Tables; }
        }
    }
}
